#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include "icapturestrategy.h"
#include "customprocessbae.h"
#include <QMainWindow>
#include <QGroupBox>
#include <QLabel>
#include <QButtonGroup>
#include <QPushButton>
#include <QTextEdit>
#include <math.h>
#include <QLineEdit>
#include <QAbstractButton>
#include <QFileDialog>
#include <QMessageBox>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <QSplineSeries>
#include <QScatterSeries>
#include <QChart>
#include <QChartView>
#include <QTimer>
#include <QTimerEvent>
#include <QtSerialPort/QSerialPort>
#include <QtSerialPort/QSerialPortInfo>
#include <future>
#include "datastorer.h"

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

#define STOP 0
#define START 1
#define OVEREXPOSURE  2

//video test3.avi
// #define WIDTH 320
// #define HEIGHT 320

// 摄像头使用
#define WIDTH 1280
#define HEIGHT 720

#define CAMERA_ID 0

#define AREA (WIDTH * HEIGHT)

struct BrightnessAnalysis
{
    float Exposure;
    float Dark;
};

struct LedBrightness
{
    std::mutex mtx;
    std::condition_variable cond;
    std::unique_ptr<uint8_t> indexBri;
};

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private slots:
    void Init();
    void OnButtonClicked(QAbstractButton *button);
    void OnUpdate();
    void timerEvent(QTimerEvent *event) Q_DECL_OVERRIDE;

private:
    Ui::MainWindow *ui;
    QTimer *m_timer;
    uint8_t state = STOP;
    bool isVideo = false;
    bool isFinish = true;
    int cnt = 0;
    QLineEdit *level[4];
    QLabel *rslt[5];
    QLabel *fpnImgLabel[3];
    int currValidPixel = 0;
    int cntOne = 1;
    cv::Mat frame2;
    QList<QImage> m_qlistLoadImgs;
    char esc;
    int numFrames = 0;

    void dataReceived(int value);
    void onComboBoxDataChanged(int index);
    void onCameraIndexChanged(int index);
    void onProcessBarClicked(int value);
    int serialSend(const uint8_t& data);
    void serialRead();
    void serialClose();
    void OverexposureCompensation(cv::Mat& gray, const BrightnessAnalysis& analysis, float threshold);
    BrightnessAnalysis exposureAnalysis(cv::Mat& image, float threshold = 20.0f, double overExposureThreshold = 250, double underExposureThreshold = 20);
    BrightnessAnalysis exposureAnalysisEx(cv::Mat& image, float threshold = 20.0f, double overExposureThreshold = 250, double underExposureThreshold = 20);
    std::vector<int> listAvailableCameras(int maxTested = 10);

    int timerId;
    int maxSize;  // data 最多存储 maxSize 个元素
    int maxX;
    int maxY;
    QList<double> data; // 存储业务数据的 list
    QLineEdit *yAxis;

    QChart *chart;
    QChartView *chartView;
    QSplineSeries *splineSeries;
    QScatterSeries *scatterSeries;
    QComboBox* seriaComboBox;
    QComboBox* cameraComboBox;
    QLabel*     itsBriLable;
    QLabel*     itsDarkLable;
    QLabel*     itsExposureLable;
    QLabel*     itsLogLable;
    QLabel*     itsRestltLable;
    CustomProgressBar *progressBar;
    std::unique_ptr<QSerialPort> itsSerialPtr;
    std::unique_ptr<std::thread> itsSerialRecvThread;
    bool                         isThreadRunning;
    uint8_t itsLedBrightness;//远端led亮度
    int cameraId;

    std::unique_ptr<ICaptureStrategy> itsVideoStragey = nullptr;

};
#endif // MAINWINDOW_H
